package com.camin.android.adapters;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.camin.android.BaseActivity;
import com.camin.android.IngredientActivity;
import com.camin.android.R;
import com.camin.android.db.DataHelper;
import com.camin.android.db.IngredientTable;
import com.camin.android.utils.Constants;

public class IngredientAdapter extends CursorAdapter {

	private Cursor mCursor;
	private Context mContext;
	private final LayoutInflater mInflater;
	
	public IngredientAdapter(Context context, Cursor cursor) {
		super(context, cursor, true);
		this.mInflater = LayoutInflater.from(context);
		this.mContext = context;
		this.mCursor = cursor;
	}

	@Override
	public void bindView(View view, Context context, Cursor cursor) {
		TextView t = (TextView) view.findViewById(R.id.txtIngredientName);
		t.setText(cursor.getString(cursor.getColumnIndex(IngredientTable.ingredientName)));
	}

	@Override
	public View newView(Context context, Cursor cursor, ViewGroup parent) {
		 final View view = mInflater.inflate(R.layout.ingredient_list_item, parent, false);
		 return view;
	}

	
	@Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if(convertView == null){
            convertView = mInflater.inflate(R.layout.ingredient_list_item, parent, false);
        }
        
        View superView = super.getView(position, convertView, parent);
        if (mCursor.moveToPosition(position)) {
        	TextView t = (TextView) convertView.findViewById(R.id.txtIngredientName);
    		t.setText(mCursor.getString(mCursor.getColumnIndex(IngredientTable.ingredientName)));
        
            ImageView imgEditButton = (ImageView) convertView.findViewById(R.id.imgEditBtn);
            imgEditButton.setTag(new Integer(position));
            imgEditButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    mCursor.moveToPosition((Integer) view.getTag());
                    Long ingredientId = mCursor.getLong(mCursor.getColumnIndex(IngredientTable._ID));
    				Intent intent = new Intent(((BaseActivity) mContext).getBaseContext(),
    						IngredientActivity.class);
    				intent.putExtra(Constants.INGREDIENT_ID, ingredientId);
    				((BaseActivity) mContext).startActivityForResult(intent, 0);
                }
            });
            
            ImageView imgDeleteButton = (ImageView) convertView.findViewById(R.id.imgDelBtn);
            imgDeleteButton.setTag(new Integer(position));
            imgDeleteButton.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                	mCursor.moveToPosition(position);
                	String ingredientName = mCursor.getString(mCursor.getColumnIndex(IngredientTable.ingredientName));
                	
                	AlertDialog.Builder adb=new AlertDialog.Builder(mContext);
                    adb.setTitle("Delete?");
                    adb.setMessage("Are you sure you want to delete " + ingredientName);
                    adb.setNegativeButton("Cancel", null);
                    adb.setPositiveButton("Ok", new AlertDialog.OnClickListener() {
                    	@Override
                    	public void onClick(DialogInterface dialog, int which) {

                    		Long ingredientId = mCursor.getLong(mCursor.getColumnIndex(IngredientTable._ID));

                    		DataHelper dh = ((BaseActivity) mContext).getDataHelper();         
                            dh.deleteIngredient(dh.getWritableDatabase(), ingredientId);
                             
                            mCursor.requery();                             
                        }});
                    adb.show();
                }});            
        }        
        return superView;
    }	
	
}
